﻿using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using SellAndBuy.Application.Query.DTOs;

namespace SellAndBuy.Infrastructure.Data.Query.Mappings
{
    public class AdDTOMap : ClassMapping<AdDTO>
    {
        public AdDTOMap()
        {
            Lazy(false);
            Table("Ad");
            //Id(x => x.ID, map => map.Generator(Generators.HighLow, gmap => gmap.Params(new {max_low = 100})));
            Id(x => x.ID);
            Property(x => x.Owner, map=> map.Formula("(SELECT c.FirstName + ' ' + c.LastName FROM Customer c INNER JOIN Ad a ON a.CustomerID = c.ID WHERE a.ID = ID)"));
            Property(x => x.Title, map=> map.Formula("(SELECT i.Name FROM Item i INNER JOIN Ad a ON a.ItemID = i.ID WHERE a.ID = ID)"));
            Property(x => x.Description, map => { map.Length(1000); map.NotNullable(true); });
            Property(x => x.Price, map => map.NotNullable(true));
            Property(x => x.CreationDate, map => map.NotNullable(true));
            Bag(x => x.Pictures,
                collectionMapping =>
                {
                    collectionMapping.Table("AdPictures");
                    collectionMapping.Access(Accessor.NoSetter);
                    collectionMapping.Cascade(Cascade.All);
                    collectionMapping.Key(k => k.Column("AdID"));
                    collectionMapping.Lazy(CollectionLazy.NoLazy);
                },
                mapping => mapping.Component(PictureInfoDTOMap.Mapping()));
        }    
    }
}